//
//  YOUfanEditProfileTopView.swift
//  NewYoufanApp
//
//  Created by 九月 on 5/31/24.
//

import UIKit


class YOUfanEditProfileTopItemView: UIControl {
    
    @objc var ttotheUserIconModel: TTotheAppUserIconList?
    
    lazy var feiwuAvatarView:UIImageView = {
        let feiwu = UIImageView()
        feiwu.contentMode = .scaleAspectFill
        feiwu.clipsToBounds = true
        return feiwu
    }()
    
    lazy var feiwuVideoView:UIImageView = {
        let feiwu = UIImageView()
        feiwu.isHidden = true
        return feiwu
    }()
    
    lazy var feiwuAddView:UIImageView = {
        let feiwu = UIImageView()
        return feiwu
    }()
    
    lazy var feiwuTypeLabel:UILabel = {
        let feiwu = UILabel()
        feiwu.isHidden = true
        feiwu.backgroundColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1).withAlphaComponent(0.5)
        feiwu.layer.cornerRadius = 2
        feiwu.layer.masksToBounds = true
        feiwu.textAlignment = .center
        feiwu.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        feiwu.text = "头像"
        return feiwu
    }()
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        cunguConfigViews()
    }
    
    func cunguConfigViews()  {
        layer.masksToBounds = true
        backgroundColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1)
        
        addSubview(feiwuAvatarView)
        addSubview(feiwuTypeLabel)
        addSubview(feiwuAddView)
        addSubview(feiwuVideoView)

    }

    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        cunguConfigViews()
    }
}

@objc protocol YOUfanEditProfileTopViewDelegate: NSObjectProtocol {
    func ttatheOnChooseImage(ttotheView: YOUfanEditProfileTopItemView)
}

@objcMembers
class YOUfanEditProfileTopView: UIView {
    
    @objc weak var delegate :YOUfanEditProfileTopViewDelegate?

    lazy var feiwuUserIconViews: [YOUfanEditProfileTopItemView] = {
        return [YOUfanEditProfileTopItemView]()
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    
    var ayiguUserIconList: [TTotheAppUserIconList]! {
        didSet {
            if 10 == 10 {
                var photoArray = [TTotheAppUserIconList]()
                var discoverVideo: TTotheAppUserIconList?
                for model in ayiguUserIconList {
                    if model.pyyouShowIndexBlue == 6 && model.hajimiVideoLinkMango.count>0 {
                        // 封面视频
                        discoverVideo = model
                    } else {
                        photoArray.append(model)
                    }
                }
                
                if discoverVideo == nil {
                    discoverVideo = TTotheAppUserIconList()
                    discoverVideo?.miyouIconBlue = ""
                    discoverVideo?.pyyouShowIndexBlue = 2
                }
                
                photoArray.insert(discoverVideo!, at: 1)
                ayiguUserIconList = photoArray
            }
            

            for index in 0..<feiwuUserIconViews.count {
                let ayigu = feiwuUserIconViews[index]
                ayigu.feiwuAddView.isHidden = false
                if index<ayiguUserIconList.count {
                    
                    let data = ayiguUserIconList[index]
                    ayigu.ttotheUserIconModel = data;
                    if (data.hajimiVideoLinkMango != nil) {
                        ayigu.feiwuAvatarView.sd_setImage(with: URL(string: data.hajimiVideoLinkMango), placeholderImage: nil)
                        ayigu.feiwuVideoView.isHidden = false
                        ayigu.feiwuAddView.isHidden = true
                    }
                    else {
                        
                        if data.miyouIconBlue.count>0 {
                            ayigu.feiwuAvatarView.sd_setImage(with: URL(string: data.miyouIconBlue!), placeholderImage: nil)
                            ayigu.feiwuVideoView.isHidden = true
                            ayigu.feiwuAddView.isHidden = true
                        }
                        else {
                            ayigu.feiwuAddView.isHidden = false
                        }
                    }
                }
            }
        }
    }
    
    @objc func ttotheOnChooseAction(ttotheBtn:YOUfanEditProfileTopItemView) {
        if let delegate = self.delegate {
            delegate.ttatheOnChooseImage(ttotheView: ttotheBtn)
        }
    }

    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

    }
}
